using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Week1.player
{
    /// <summary>
    /// This class displays a mouse pointer that can be contolled using arrow kesy (moust not supported).
    /// This class is used to select objects on the screen such as towers, enemies, etc
    /// This class depends on TheGrid class.
    /// </summary>
    public class ObjectSelector : Microsoft.Xna.Framework.DrawableGameComponent, helper.IDebugable
    {
        // grid selector unit size. usually 1.
        // movement bounds. It restricts grid selection beyond certain bound. It depends on the camera position.
        // grid selector current position
        // grid pointer using primitive or texture (decision pending). object selection state is managed by object not this selector.
        // grid selector move speed
        // grid size rows x cols reference. This object is provided the TheGrid class that defines the train height.
        // grid height map reference. this object is provided by TheGrid class that defines the train height.

        Camera oCamera;
        MousePointer oPointer;
        helper.DebugConsole oDebug;

        // For debug only. this can visualize the grid using set of lines. It is similar to the TheGrid class.
        bool oDebugEnabled = false;
        world.DrawPrimitive oGridDebugger;

        public ObjectSelector(Game game, Camera camera, MousePointer pointer)
            : base(game)
        {
            oCamera = camera;
            oPointer = pointer;
            //oGridDebugger = new world.DrawPrimitive(this.Game, oCamera, 4, PrimitiveType.LineList);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            oGridDebugger.Initialize();
            base.Initialize();
        }

        protected override void LoadContent()
        {
            oDebug = helper.DebugConsole.Instance;
            oDebug.registerDebug(this);
            oGridDebugger.forceLoadContent();

            /*oGridDebugger.setPointAt(0, new Vector3(0, 0, 0), Color.Green);
            oGridDebugger.setPointAt(1, new Vector3(1, 150, 200), Color.Green);
            oGridDebugger.setPointAt(2, new Vector3(2, 0, 0), Color.Violet);
            oGridDebugger.setPointAt(3, new Vector3(3, 150, 200), Color.Violet);
            */
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            /*oGridDebugger.setPointAt(1, oCamera.Position.X, oCamera.Position.Y -.1f , oCamera.Position.Z- .1f, Color.Blue); 

            if(Keyboard.GetState().IsKeyDown(Keys.F12))
            {
                oGridDebugger.setPointAt(2, oGridDebugger.getPointAt(0), Color.Violet);
                oGridDebugger.setPointAt(3, oGridDebugger.getPointAt(1), Color.Violet);
            }
            */
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            // this debug flag is separate from debug implementation
            //if (oDebugEnabled)
            oGridDebugger.Draw(gameTime);

            base.Draw(gameTime);
        }

        /// <summary>
        /// Debugger implementation
        /// </summary>
        helper.DebugMsg oMsg = new helper.DebugMsg();

        helper.DebugMsg helper.IDebugable.getDebugMsg(bool showNextInstance)
        {
            oMsg.message = "line0=" + oGridDebugger.getPointAt(0);
            oMsg.message += "\nline1=" + oGridDebugger.getPointAt(1);
            oMsg.message += "\ncamera=" + oCamera.Position;
            oMsg.position = oGridDebugger.getPointAt(0);
            return oMsg;
        }
    }
}
